
using System;
using System.Collections.Generic;
namespace ImryStefanVRPprogram
{
    public class VehicleRoute
    {
        // Fields and Properties

        /// <summary>
        /// Stores the nodes visited along the route. Must start and end with a zero, to signify the depot.
        /// </summary>
        public List<int> nodesArray { get; set; }


        public double _totalDemandOnRoute;
        public double totalDemandOnRoute {
            get
            {
                return _totalDemandOnRoute;
            }
            set
            {
                _totalDemandOnRoute = value;
                if (Program.DEBUG)
                {
                    double realTotalDemandOnRoute = 0;
                    foreach (int i in nodesArray)
                        realTotalDemandOnRoute += Program.vrpData.Demands[i];
                    if (Math.Abs(realTotalDemandOnRoute - value) > 1e-6)
                        throw new Exception("Assigning bad demand");
                }
            }
        }

        public double _totalDistanceOfRoute;
        public double totalDistanceOfRoute
        {
            get
            {
                return _totalDistanceOfRoute;
            }
            set
            {
                _totalDistanceOfRoute = value;
                if (Program.DEBUG)
                {
                    double realTotalDistanceOnRoute = 0;
                    for(int i=0 ; i < GetNumberOfNodesVisited()+1;i++)
                        realTotalDistanceOnRoute += Program.vrpData.Distances[nodesArray[i],nodesArray[i+1]];
                    if (Math.Abs(realTotalDistanceOnRoute - value) > 1e-6)
                        throw new Exception("Assigning bad distance");
                }
            }
        }

        // Constructors 
        public VehicleRoute(int nodeIndex)
        {
            nodesArray = new List<int>();
            nodesArray.Add(0);
            nodesArray.Add(nodeIndex);
            nodesArray.Add(0);
        }

        public VehicleRoute(List<int> tour)
        {
            nodesArray = tour;
        }

        // Methods

        public int GetNumberOfNodesVisited()
        {
            return nodesArray.Count - 2;
        }

        internal int GetNodeAt(int j)
        {
            return nodesArray[j];
        }

        internal int GetLastNodeVisited()
        {
            return nodesArray[nodesArray.Count - 2];
        }

        internal void RemoveNodeAt(int j)
        {
            nodesArray.RemoveAt(j);
        }

        internal void InsertNodeAt(int j, int nodeActualValue)
        {
            nodesArray.Insert(j, nodeActualValue);
        }

        internal void SwitchNode(int j, int nodeActualValue)
        {
            nodesArray[j] = nodeActualValue;
        }

    }
}